<?php

class Friendsmodel extends Model {
	
		function isConfirmed($userId, $friendId)
		{
				$sql = "SELECT friends_list_id FROM friends_list WHERE (approved_status='yes' AND user_id=" . $userId . " AND friend_id=" . $friendId . ") OR (approved_status='yes' AND user_id=" . $friendId . " AND friend_id=" . $userId .")";
				$res = $this->db->query($sql);
				if ($res->num_rows() > 0) 
				{
					return true;
				}
		}
		
		function isWaitConfirmed($userId, $friendId)
		{
				$sql = "SELECT friends_list_id FROM friends_list WHERE (approved_status='no' AND user_id=" . $userId . " AND friend_id=" . $friendId . ") OR (approved_status='no' AND user_id=" . $friendId . " AND friend_id=" . $userId .")";
				$res = $this->db->query($sql);
				if ($res->num_rows() > 0) 
				{
					return true;
				}
		}
		
		function newInvitation($friendEmail, $friendId)
		{
						$status = 'no';
						$userId = $this->session->userdata('userId');
						$newSQL = 'INSERT INTO friends_list
							(user_id, approved_status, friend_id, friend_mail,datestamp)
							VALUES
							(	' . $userId . ',
								' . $this->db->escape($status) . ',
								' . $this->db->escape($friendId) . ',
								' . $this->db->escape($friendEmail) . ',
								CURRENT_TIMESTAMP
							)';
						$this->db->query($newSQL);
						if ($this->db->affected_rows() == 1)
						{
								$inviteId = $this->db->insert_id();
								return $inviteId;
						}
						else  return false;
				
		} //end newInvitation()
		
		function getRequest($userId = '')
		{
				if ($userId == '') $userId = $this->session->userdata('userId');
				$this->db->select('friends_list.friends_list_id, users.user_id, users.avatar, users.username');
				$this->db->from('friends_list');
				$this->db->join('users', 'friends_list.user_id=users.user_id', 'INNER');
				$this->db->where('friends_list.friend_id', $userId);
				$this->db->where('friends_list.approved_status', 'no');
				$friendRequestQuery = $this->db->get();
				$friendRequest = array();
				if ($friendRequestQuery->num_rows() > 0)
				{
						foreach ($friendRequestQuery->result_array() as $friendRequestRow)
						{
								//$friendRequestRow['avatar'] = getAvatar($friendRequestRow['user_id'], $friendRequestRow['avatar_ext'], 'thumb');
								$friendRequest[] = $friendRequestRow;
						}
				}
				return $friendRequest;
		}

		function getRequestCount()
		{
				$userId = $this->session->userdata('userId');
				$requestCountQuery = $this->db->query('SELECT friends_list_id FROM friends_list WHERE approved_status=\'no\' AND friend_id = ' . $userId . ' GROUP BY friends_list_id');
				return $requestCountQuery->num_rows();
		}
		
		function acceptRequest($requestId)
		{
				$this->db->where('friends_list_id', $requestId);
				$this->db->update('friends_list', array('approved_status' => 'yes'));
		}
		
		function rejectRequest($requestId)
		{
				$this->db->where('friends_list_id', $requestId);
				$this->db->delete('friends_list');
		}
		
		function cancelFriend($friendId)
		{
						$sql = 'DELETE FROM friends_list WHERE (user_id=' . $this->session->userdata('userId') . ' AND friend_id=' . $friendId . ') OR (user_id=' . $friendId . ' AND friend_id=' . $this->session->userdata('userId') . ')';
						$res = $this->db->query($sql);
						if ($this->db->affected_rows() > 0) return true;
						else  return false;
		} //end deleteFriend()
}